package com.woniu.market_stall.web;

import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.AuctionStallService;
import com.woniu.market_stall.vo.AuctionStallVo;
import com.woniu.market_stall.vo.StallVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("actionStall")
public class AuctionStallController {

    @Autowired
    AuctionStallService auctionStallService;

    @GetMapping("/list")
    @ApiOperation(value = "本接口是查询按条件分页查询竞拍摊位数据的")
    public ResultResponse<PageInfo<AuctionStallVo>> list(
            @ApiParam(value = "当前页的参数") @RequestParam(value = "now",defaultValue = "1") Integer now,
            @ApiParam(value = "页面大小的参数") @RequestParam(value = "size",defaultValue = "3")Integer size,
            @ApiParam(value = "按条件搜索的摊位名称") @RequestParam(value = "stallName",defaultValue = "") String stallName ,
            @ApiParam(value = "按条件搜索的摊位状态") @RequestParam(value = "status",defaultValue = "") String status,
            @ApiParam(value = "按条件搜索的市场的id") @RequestParam(value = "marketId",defaultValue = "0") String marketId){
        ResultResponse<PageInfo<AuctionStallVo>> result = null;
        try {
            PageInfo<AuctionStallVo> pageInfo = auctionStallService.queryAuctionStallByPage(now, size, status ,marketId , stallName );
            result = new ResultResponse<>(200,"查询竞拍摊位成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询竞拍摊位失败!");
        }
        return result;
    }


    @PutMapping("/add")
    @ApiOperation(value = "本接口是添加新的竞拍摊位的")
    public ResultResponse<Void> add(@RequestBody AuctionStallVo auctionStallVo) {
        ResultResponse<Void> result = null;
        try {
            auctionStallService.add(auctionStallVo);
            result = new ResultResponse<>(200, "添加竞拍摊位成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加竞拍摊位失败");
        }
        return result;
    }


    /**
     * 修改 竞拍摊位信息
     *
     * @param stallVo
     * @return
     */
    @PutMapping("/modify")
    @ApiOperation(value = "本接口是修改竞拍摊位")
    public ResultResponse<Void> modify(@RequestBody AuctionStallVo auctionStallVo) {
        ResultResponse<Void> result = null;
        try {
            auctionStallService.modify(auctionStallVo);
            result = new ResultResponse<>(200, "修改竞拍摊位成功!");

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改竞拍摊位失败!");
        }
        return result;
    }


    /**
     * 修改 竞拍摊位信息
     *
     * @param id 需要删除的id
     * @return
     */
    @PutMapping("/del/{id}")
    @ApiOperation(value = "本接口是删除竞拍摊位")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            auctionStallService.del(id);
            result = new ResultResponse<>(200, "删除竞拍摊位成功!");

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除竞拍摊位失败!");
        }
        return result;
    }

}
